<template>
  <div class="container">
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
      class="diglogstyle"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="活动区域" prop="subjectID">
          <el-select v-model="ruleForm.subjectID" placeholder="请选择活动区域">
            <el-option
              v-for="(item, index) in data"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="活动名称" prop="name">
          <el-input v-model="ruleForm.tagName"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { simple } from "@/api/hmmm/subjects.js";
import { add, update } from "@/api/hmmm/tags.js";
export default {
  props: {
    dialogVisible: {},
    updataData: {},
  },

  data() {
    return {
      data: null,
      ruleForm: {
        subjectID: "",
        tagName: "",
      },
      rules: {
        tagName: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getData();
  },
  watch: {
    updataData(v) {
      if (v) {
        this.btnStatus = "update";
        this.ruleForm = Object.assign({}, v);
        // this.ruleForm.tagName = v.tagName;
        // this.ruleForm.subjectID = v.subjectID;
      } else {
        this.btnStatus = "add";
      }
    },
  },
  methods: {
    async getData() {
      const { data } = await simple();
      this.data = data;
    },
    handleClose() {
      this.$emit("handleClose");
    },
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          if (this.btnStatus !== "update") {
            await add(this.ruleForm);
          } else {
            await update(this.ruleForm);
          }
          this.ruleForm = {
            subjectID: "",
            tagName: "",
          };
          this.$emit("handleOk");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style>
.container {
  margin: 10px 10px;
}
</style>
